package com.atguigu.dga.governance.assessor.storage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component("TABLE_SIMILAR")
public class TableSimilarAssessor  extends Assessor {
    @Override
    protected void assessProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {
        //1 当前表 和 其他所有表的所有字段 进行比对   表和表之间  =》 重复个数
         // 1 1层 所有表
        //    2层  两个字段列表 两层循环比较
        TableMetaInfo tableMetaInfoSelf = assessParam.getTableMetaInfo();
        List<TableMetaInfo> tableMetaInfoAllList = assessParam.getTableMetaInfoAllList();

        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramJsonObj = JSON.parseObject(metricParamsJson);
        Integer paramPercent = paramJsonObj.getInteger("percent");

        Set similarTableSet=new HashSet();

        for (TableMetaInfo tableMetaInfoOther : tableMetaInfoAllList) {
            if( tableMetaInfoSelf.getTableName().equals(tableMetaInfoOther.getTableName())
                &&tableMetaInfoSelf.getSchemaName().equals(tableMetaInfoOther.getSchemaName())
            ){
                continue;  //跳过自己
            }
            if(!tableMetaInfoSelf.getTableMetaInfoExtra().getDwLevel().equals(tableMetaInfoOther.getTableMetaInfoExtra().getDwLevel())){
                continue;  //不同层次不做比较
            }
            if(tableMetaInfoSelf.getTableMetaInfoExtra().getDwLevel().equals("ODS")){
                continue;
            }

            Integer sameColCount=0;

            // 两个字段列表 两层循环比较
            String colNameJsonSelf = tableMetaInfoSelf.getColNameJson();
            List<JSONObject> colJsonListSelf = JSON.parseArray(colNameJsonSelf, JSONObject.class);

            String colNameJsonOther = tableMetaInfoOther.getColNameJson();
            List<JSONObject> colJsonListOther = JSON.parseArray(colNameJsonOther, JSONObject.class);
            for (JSONObject colJsonObjSelf : colJsonListSelf) {
                for (JSONObject colJsonObjOther : colJsonListOther) {
                    String colNameSelf = colJsonObjSelf.getString("name");
                    String colNameOther = colJsonObjOther.getString("name");
                    if(colNameSelf.equals(colNameOther)){
                        sameColCount++;
                    }
                }
            }
            // 2重复字段个数/总字段个数 =%
            int samePercent = sameColCount*100 / colJsonListSelf.size();
            int sameOtherPercent = sameColCount*100 / colJsonListOther.size();
            // 3 和参数percent比较  大于则给0分
            if(samePercent>paramPercent&&sameOtherPercent >paramPercent){
                similarTableSet.add(tableMetaInfoOther.getSchemaName()+"."+tableMetaInfoOther.getTableName());
            }
        }

        if(similarTableSet.size()>0){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("存在相似表：["+ StringUtils.join(similarTableSet,",")+"]" );
        }
    }
}
